package com.twoGroup.boot.controller;

import com.twoGroup.boot.pojo.CartAndShopAndUserAndGoods;
import com.twoGroup.boot.pojo.Order;
import com.twoGroup.boot.pojo.OrderAndAddress;
import com.twoGroup.boot.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/Order")
@Api(tags = "订单接口")
public class OrderController {

        public static Logger logger = LoggerFactory.getLogger(OrderController.class);
        @Resource
        private OrderService orderService;


        //购物车中购买
        @ApiOperation(value = "生成订单：在购物车内购买", notes = "需要用户id 直接将购物车所用商品进行结算 取值时session.OrderInCartSession.xxx  总价格取值时session.allPriceInCart")
        @PostMapping("/addOrderInCart")
        @ResponseBody
        public List<OrderAndAddress> addOrderInCart(Integer userId, Integer addressId, HttpServletRequest request){
            List<OrderAndAddress> orderAndAddresses = null;
            List<Order> orders = null;
            String orderNo = "";
            float allPrice = 0;
            //1、用户购物车选中了什么商品
            try {
                List<CartAndShopAndUserAndGoods> cartAndShopAndUserAndGoods = orderService.queryUserCart(userId);
                orders = new ArrayList<>();
                orderAndAddresses = new ArrayList<>();
                for (CartAndShopAndUserAndGoods cart : cartAndShopAndUserAndGoods){
                    Date date = new Date(System.currentTimeMillis());
                    orderNo = System.currentTimeMillis()+""+userId+cart.getGoodsId()+addressId;
                    //输出总价格
                    allPrice += cart.getGoodsPrice() * cart.getGoodsCount();
                    //封装到对象
                    Order order = new Order(null,cart.getShopId(),cart.getGoodsId(),userId,orderService.queryShopId(cart.getShopId()), String.valueOf(date),(cart.getGoodsPrice() * cart.getGoodsCount()),cart.getGoodsCount(),orderNo,0,addressId);
                    //3、将用户购物车中选中的商品添加到订单表中，
                    int i = orderService.addOrder(order);
                    OrderAndAddress orderAndAddress = orderService.queryInfAndAddress(orderNo);
                    orders.add(order);
                    orderAndAddresses.add(orderAndAddress);
                }
                request.getSession().setAttribute("OrderInCartSession",orderAndAddresses);
                return orderAndAddresses;

            } catch (Exception e) {
                e.printStackTrace();
            }
            return orderAndAddresses;
        }

        //在页面上直接购买，不经过购物车
        @ApiOperation(value = "生成订单：在页面上购买，不经过购物车", notes = "需要用户id 商品id 店铺id 商品价格 商品数量 ； 直接将购物车所用商品进行结算 取值时session.OrderInPageSession.xxx  总价格取值时session.allPriceInCart")
        @PostMapping("/addOrderInPage")
        @ResponseBody
        public OrderAndAddress addOrderInPage(Model model, Integer userId, Integer goodsId, Integer shopId, float goodsPrice, Integer goodsCount, Integer addressId, HttpServletRequest request){
            //1.订单号的唯一性
            String date = System.currentTimeMillis()+"";
            String orderNo = String.valueOf(System.currentTimeMillis())+userId+goodsId+addressId;
            logger.info("orderNo-------1------>"+orderNo);
            //2.新建订单对象
            Order order = new Order(null,shopId,goodsId,userId,orderService.queryShopId(shopId),date,(goodsCount * goodsPrice),goodsCount,orderNo,0,addressId);
            //3、存入订单表
            int i = orderService.addOrder(order);
            logger.info("order-------------->"+i);
            //遍历出订单
            logger.info("orderNo-------2------>"+orderNo);
            OrderAndAddress orderAndAddress = orderService.queryInfAndAddress(orderNo);
//                Order orders = orderService.queryId(userId, goodsId,orderNo);
            logger.info(String.valueOf(orderAndAddress));
            //4、存入session域
            request.getSession().setAttribute("OrderInPageSession",orderAndAddress);
                return orderAndAddress;
        }

        //删除某个订单(已支付)
        @ApiOperation(value = "删除某个订单(已支付)", notes = "传入用户id 商品id 商品订单号（orderNo）")
        @PostMapping("/deleteOrderY")
        @ResponseBody
        public int deleteOrderY(Integer userId, Integer goodsId, String orderNo){
            return orderService.deleteOrderY(userId, goodsId, orderNo);
        }

        //删除所有订单（已支付）
        @ApiOperation(value = "删除所有订单（已支付）", notes = "传入用户id")
        @PostMapping("/deleteAllOrderY")
        @ResponseBody
        public int deleteAllOrderY(Integer userId){
            return  orderService.deleteAllOrderY(userId);
        }

        //删除某个订单(未支付)
        @ApiOperation(value = "删除某个订单(未支付)", notes = "传入用户id 商品id 商品订单号（orderNo）")
        @PostMapping("/deleteOrderN")
        @ResponseBody
        public int deleteOrderN(Integer userId, Integer goodsId, String orderNo){
            return orderService.deleteOrderN(userId, goodsId, orderNo);
        }

        //删除所有订单（未支付）
        @ApiOperation(value = "删除所有订单（未支付）", notes = "传入用户id")
        @PostMapping("/deleteAllOrderN")
        @ResponseBody
        public int deleteAllOrderN(Integer userId){
           return orderService.deleteAllOrderN(userId);
        }

        //查询所有订单(已支付)
        @ApiOperation(value = "查询所有订单（已支付）", notes = "传入用户id")
        @PostMapping("/queryAllOrderY")
        @ResponseBody
        public List<OrderAndAddress> queryAllOrderY(Integer userId){
            return orderService.queryAllOrderY(userId);
        }

        /**
         * 查询所有订单（未支付）
         * @param userId id
         * @return orders
         */
        @ApiOperation(value = "查询所有订单（未支付）", notes = "传入用户id")
        @PostMapping("/queryAllOrderN")
        @ResponseBody
        public List<OrderAndAddress> queryAllOrderN(Integer userId){
            return orderService.queryAllOrderN(userId);
        }

        @ApiOperation(value = "查询所有订单（已支付）返回订单数量", notes = "传入用户id")
        @PostMapping("/queryAllOrderCountY")
        @ResponseBody
        public Integer queryAllOrderCountY(Integer userId){
            return orderService.queryAllOrderCountY(userId);
        }

        @ApiOperation(value = "查询所有订单（未支付）返回订单数量", notes = "传入用户id")
        @PostMapping("/queryAllOrderCountN")
        @ResponseBody
        public Integer queryAllOrderCountN(Integer userId){
            return orderService.queryAllOrderCountN(userId);
        }

}
